package com.parkinglot.service.Impl;

import com.parkinglot.dao.AdvertDao;
import com.parkinglot.dao.AdvertRecordDao;
import com.parkinglot.parmeter.AdvertqueryParm;
import com.parkinglot.parmeter.UserqueryParm;
import com.parkinglot.pojo.Advert;
import com.parkinglot.pojo.AdvertRecord;
import com.parkinglot.pojo.User;
import com.parkinglot.service.AdvertRecordService;
import com.parkinglot.vo.AdvertCountVo;
import com.parkinglot.vo.UserAdvertqueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * (AdvertRecord)表服务实现类
 *
 * @author makejava
 * @since 2020-11-13 15:28:19
 */
@Service("advertRecordService")
public class AdvertRecordServiceImpl implements AdvertRecordService {
    @Resource
    private AdvertRecordDao advertRecordDao;

    @Resource
    private AdvertDao advertDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public AdvertRecord queryById(Integer id) {
        return this.advertRecordDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<AdvertRecord> queryAllByLimit(int offset, int limit) {
        return this.advertRecordDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param advertRecord 实例对象
     * @return 实例对象
     */
    @Override
    public AdvertRecord insert(AdvertRecord advertRecord) {
        this.advertRecordDao.insert(advertRecord);
        return advertRecord;
    }

    /**
     * 修改数据
     *
     * @param advertRecord 实例对象
     * @return 实例对象
     */
    @Override
    public AdvertRecord update(AdvertRecord advertRecord) {
        this.advertRecordDao.update(advertRecord);
        return this.queryById(advertRecord.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.advertRecordDao.deleteById(id) > 0;
    }

    @Override
    public List<UserAdvertqueryVo> advertrecord(UserqueryParm userqueryParm) {
        User u = new User();
        List<UserAdvertqueryVo> list = null;
        try {
            BeanUtils.copyProperties(userqueryParm, u);
            list = advertRecordDao.advertrecord(u);
            if (list.size() > 0) {
                return list;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Collections.emptyList();
    }

    /**
     * 统计所有广告投放数或某个车库广告投放数
     *
     * @param advertqueryParm
     * @return
     */
    @Override
    public AdvertCountVo countadvert(AdvertqueryParm advertqueryParm) {
        Advert advert = new Advert();
        AdvertCountVo advertCountVo = null;
        try {
            BeanUtils.copyProperties(advertqueryParm, advert);
            advertCountVo = advertDao.countadvert(advert);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return advertCountVo;
    }

    @Override
    public List<AdvertCountVo> garageradvert(AdvertqueryParm advertqueryParm) {
        Advert advert = new Advert();

        List<AdvertCountVo> list = null;

        BeanUtils.copyProperties(advertqueryParm, advert);
        list = advertDao.garageradvert(advert);

        if (list.size() > 0)
            return list;

        return Collections.emptyList();
    }
}